package com.online_exam.controller;

import com.online_exam.base.RestResponse;
import com.online_exam.domain.User;
import com.online_exam.service.UserService;
import com.online_exam.util.JwtUtil;
import com.online_exam.viewmodel.student.user.LoginVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController("userController")
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public RestResponse login(@RequestBody LoginVM loginVM, HttpServletResponse response){
        String userName = loginVM.getUserName();
        String password = loginVM.getPassword();
        if (true){
            return RestResponse.ok();

        }
        User user = userService.selectByUsernamePassword(userName, password);
        if (user!=null){

            Map map = new HashMap();
            map.put("id", user.getId().toString());
            map.put("userName", user.getUserName());
            String token = JwtUtil.getToken(map);
            response.setHeader("token",token);
            return RestResponse.ok();
        }
        return RestResponse.fail(2, "用户名或密码错误！");
    }

    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public RestResponse logout(){
        return RestResponse.ok();
    }
}
